package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.CouponConstants;
import com.ruoyi.common.constant.SysConfigConstants;
import com.ruoyi.common.entity.DO.marketing.CouponDO;
import com.ruoyi.common.entity.DO.product.ProductDO;
import com.ruoyi.common.entity.DO.product.ShopProductDO;
import com.ruoyi.common.entity.VO.shop.ShopConfigVO;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IAsyncProductService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service("AsyncProductServiceImpl")
public class AsyncProductServiceImpl implements IAsyncProductService {

    @Resource
    private IBackShopConfigMapper shopConfigMapper;

    @Resource
    private IBackProductMapper productMapper;

    @Resource
    private IAsyncProductCategoryMapper categoryMapper;

    @Resource
    private IBackShopProductMapper shopProductMapper;

    @Resource
    private IAsyncShopCouponMapper asyncShopCouponMapper;

    @Override
    public void productEnableAsyncToShop(Long[] productIdArr) {
        List<ShopProductDO> shopProductList = new ArrayList<>();
        //TODO：获取商家设置-店铺商品价格上浮比例
        List<ShopConfigVO> shopConfigList = shopConfigMapper.searchListByKey(SysConfigConstants.GlobalConfigEnum.SHOP_PRODUCT_PRICE_RATE.getKey());
        boolean isAsync;
        Date now = new Date();
        String createBy = "平台上架自动同步";
        //TODO：处理普通关联
        for (Long productId : productIdArr
        ) {
            List<Long> shopIdList = productMapper.searchShopProductList(productId);
            if (shopConfigList.size() == shopIdList.size()) {
                continue;
            }
            ProductDO productDO = productMapper.getProductForSync(productId);
            if (productDO != null) {
                for (ShopConfigVO shopConfigVO : shopConfigList) {
                    isAsync = false;
                    for (Long shopId : shopIdList
                    ) {
                        if (shopConfigVO.getShopId().equals(shopId)) {
                            isAsync = true;
                            break;
                        }
                    }
                    if (isAsync) {
                        continue;
                    }
                    ShopProductDO shopProductDO = new ShopProductDO();
                    shopProductDO.setProductId(productId);
                    shopProductDO.setOrderNum(productDO.getOrderNum());
                    shopProductDO.setIsEnable(1);
                    shopProductDO.setShopId(shopConfigVO.getShopId());
                    shopProductDO.setCreateTime(now);
                    shopProductDO.setCreateBy(createBy);
                    shopProductList.add(shopProductDO);
                }
            }
        }
        //TODO:入库
        if (shopProductList.size() > 0) {
            shopProductMapper.multiInsert(shopProductList);
        }
    }

    @Override
    public void productCategoryNameChangeEvent(Long categoryId) {
        List<ProductDO> productList = categoryMapper.listProductLinkCategoryId(categoryId);
        if (productList.size() > 0)
            for (ProductDO one : productList
            ) {
                productMapper.updateByPrimaryKeySelective(one);
            }
    }

    @Override
    public void productSaleAsync(String orderSn, Long shopId) {

    }

    @Override
    public void refreshCoupon(String couponIdStr) {
        List<CouponDO> couponList;
        Example example = new Example(CouponDO.class);
        example.createCriteria().andEqualTo("isDelete", 0);
        example.createCriteria().andIn("couponStatus", Arrays.asList(CouponConstants.CouponStatusEnum.AVAILABLE.getValue(),CouponConstants.CouponStatusEnum.FINISH.getValue()));
        if (StringUtils.isNotEmpty(couponIdStr)) {
            example.createCriteria().andIn("id", Arrays.asList(couponIdStr.split(",")));
        }
        couponList = asyncShopCouponMapper.selectByExample(example);
        if (couponList.size() > 0) {
            couponList.forEach(map->{
                //TODO:更新已领取数量，库存
                asyncShopCouponMapper.refreshTotalGet(map.getId());
                //TODO:更新已使用数量
                asyncShopCouponMapper.refreshTotalUse(map.getId());
                //TODO:更新库存
                asyncShopCouponMapper.refreshStock(map.getId());
            });
        }
        //优惠券刷新
        asyncShopCouponMapper.waitGetCoupon();
        asyncShopCouponMapper.nowGettingCoupon();
        asyncShopCouponMapper.getOverCoupon();
        asyncShopCouponMapper.expireCoupon();
        asyncShopCouponMapper.expireMemberCoupon();
    }
}
